package tw.asap.plus.ui.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.entity.response.ShopIndex;
import tw.asap.plus.utils.T;

/**
 * Description:
 * Created:      [15/11/9]
 * Author:       [Yang]
 **/
public class SimpleGridViewAdapter extends BaseAdapter {

    Context context;
    List<ShopIndex.ClassListEntity> list;
    private int[] holderImages = {R.drawable.temp_bg_1, R.drawable.temp_bg_2, R.drawable.temp_bg_3,
            R.drawable.temp_bg_4, R.drawable.temp_bg_5, R.drawable.temp_bg_6, R.drawable.temp_bg_7,
            R.drawable.temp_bg_8};

    public SimpleGridViewAdapter(Context context, List<ShopIndex.ClassListEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list != null ? list.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_menu, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (TextUtils.isEmpty(list.get(position).getImg())) {
            holder.image.setBackgroundResource(holderImages[position % 7]);
        } else {
            holder.image.setImageURI(Uri.parse(list.get(position).getImg()));
        }
        holder.title.setText(list.get(position).getScname());
        return convertView;
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_menu.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @InjectView(R.id.image)
        SimpleDraweeView image;
        @InjectView(R.id.title)
        TextView title;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
